home *** CD-ROM | disk | FTP | other *** search
- *****Listing 1*****
-
-
- /*************************************************************
- *
- * file d:\tc\cujds.c
- *
- * Functions: This file contains
- * main
- * display_belief_vector
- * clear_belief_vector
- * enter_belief_vector
- * combine_using_dempsters_rule
- *
- * Purpose:
- * This program demonstrates how to implement Dempster's
- * rule of combination.
- *
- * NOTE: This is written for Borland's Turbo C
- * Version 1.5. This allows us to use some
- * nice user interface functions. The actual
- * combination code is compiler independent.
- *
- *************************************************************/
-
-
- extern unsigned int _stklen = 40000;
-
-
- #include "d:\tc\include\stdio.h"
- #include "d:\tc\include\io.h"
- #include "d:\tc\include\fcntl.h"
- #include "d:\tc\include\dos.h"
- #include "d:\tc\include\math.h"
- #include "d:\tc\include\graphics.h"
- #include "d:\tc\include\conio.h"
- #include "d:\tc\include\sys\stat.h"
-
- #define LENGTH_OF_BELIEF_VECTOR 8
-
-
-
-
- main()
- {
-
- char response[80];
-
- int choice,
- i,
- j,
- not_finished;
-
- short place;
-
-
- float a[LENGTH_OF_BELIEF_VECTOR],
- belief,
- v[LENGTH_OF_BELIEF_VECTOR];
-
-
-
- textbackground(1);
- textcolor(7);
- clrscr();
-
- not_finished = 1;
- while(not_finished){
-
- clrscr();
- printf("\n> You may now either:");
- printf("\n 1. Start the process");
- printf("\n 2. Enter more assertions");
- printf("\n 3. Exit program");
- printf("\n _\b");
- get_integer(&choice);
-
- switch (choice){
-
- case 1:
- clear_belief_vector(v);
- clear_belief_vector(a);
- clrscr();
- enter_belief_vector(v, 1);
- clrscr();
- enter_belief_vector(a, 1);
- clrscr();
- printf("\n> Initial Belief Vector\n");
- display_belief_vector(v);
- printf("\n> Second Belief Vector\n");
- display_belief_vector(a);
- combine_using_dempsters_rule(v, a);
- printf("\n> Resultant Belief Vector\n");
- display_belief_vector(v);
- break;
-
- case 2:
- clrscr();
- clear_belief_vector(a);
- enter_belief_vector(a, 1);
- clrscr();
- printf("\n> Initial Belief Vector\n");
- display_belief_vector(v);
- printf("\n> Second Belief Vector\n");
- display_belief_vector(a);
- combine_using_dempsters_rule(v, a);
- printf("\n> Resultant Belief Vector\n");
- display_belief_vector(v);
- break;
-
- case 3:
- not_finished = 0;
- break;
-
- } /* ends switch choice */
- } /* ends while not_finished */
- } /* ends main */
-
-
-
- clear_belief_vector(v)
- float v[];
- {
- int i;
-
- for(i=0; i<LENGTH_OF_BELIEF_VECTOR; i++)
- v[i] = 0.0;
- } /* ends clear_belief_vector */
-
-
-
-
- display_belief_vector(v)
- float v[];
- {
- int i, j;
- char response[80];
-
- j=1;
- for(i=0; i<LENGTH_OF_BELIEF_VECTOR; i++){
- if((j%5) == 0){
- printf("\n");
- j++;
- }
- if(v[i] > 0.0001){
- printf(" [%3d]=%6f", i, v[i]);
- j++;
- }
- }
- printf("\n Hit RETURN to continue");
- read_string(response);
- } /* ends display_belief_vector */
-
-
-
-
- enter_belief_vector(v, line)
- float v[];
- int line;
- {
- int i,
- not_finished,
- y;
- float value;
- y = line;
-
- printf("\n> ENTER BELIEF VECTOR");
-
- printf("\n> Enter the place (RETURN) and value (RETURN)");
- printf("\n> (Enter -1 for place when you're finished)");
-
- not_finished = 1;
- while(not_finished){
- printf("\n [___]=______");
- y = wherey();
- gotoxy(5, y);
- get_integer(&i);
- gotoxy(10, y);
- get_float(&value);
-
- if(i != -1){
- v[i] = value;
- } /* ends if i 1+ -1 */
- else
- not_finished = 0;
-
- } /* ends while not_finished */
- } /* ends enter_belief_vector */
-
-
-
-
- /************************************************************
- *
- * This is the function that implements Demptser's rule
- * of combination.
- * vector1 holds the original beliefs and will hold the
- * result of the combination.
- *
- ************************************************************/
-
- combine_using_dempsters_rule(vector1, vector2)
- float vector1[LENGTH_OF_BELIEF_VECTOR],
- vector2[LENGTH_OF_BELIEF_VECTOR];
- {
- float denominator,
- sum_vector[LENGTH_OF_BELIEF_VECTOR];
-
- int a,
- i,
- place;
-
- /* set the sums to zero */
- for(i=0; i<LENGTH_OF_BELIEF_VECTOR; i++)
- sum_vector[i] = 0.0;
-
- /* Now go through the intersection tableau. */
- /* Look for the intersection of non-zero beliefs */
- /* and save their products. */
-
- for(a=1; a<LENGTH_OF_BELIEF_VECTOR; a++){
- if(vector2[a] > 0.0){
- for(i=0; i<LENGTH_OF_BELIEF_VECTOR; i++){
- place = i & a;
- if(vector1[i] > 0.0)
- sum_vector[place] = (vector1[i] * vector2[a]) + sum_vector[place];
- } /* ends loop over i */
- } /* ends if vector2[a] > 0.0 */
- } /* ends loop over a */
-
- denominator = 1.0 - sum_vector[0];
- for(i=1; i<LENGTH_OF_BELIEF_VECTOR; i++)
- vector1[i] = sum_vector[i]/denominator;
-
-
- } /* ends combine_using_dempsters_rule */
-
-
-
-
-
-
- /* The following functions are I-O */
-
-
-
- read_string(string)
- char *string;
- {
- int eof,
- letter,
- no_error;
-
- eof = -1;
- no_error = 0;
-
- while((letter = getchar()) != '\n' &&
- letter != eof)
- *string++ = letter;
-
- *string = '\0';
-
- return((letter == eof) ? eof : no_error);
-
- } /* ends read_string */
-
-
-
- clear_buffer(string)
- char string[];
- {
- int i;
- for(i=0; i<80; i++)
- string[i] = ' ';
- }
-
-
-
- long_clear_buffer(string)
- char string[];
- {
- int i;
- for(i=0; i<300; i++)
- string[i] = ' ';
- }
-
-
-
-
- #define is_digit(x) ((x >= '0' && x <= '9') ? 1 : 0)
-
- #define is_blank(x) ((x == ' ') ? 1 : 0)
-
- #define to_decimal(x) (x - '0')
-
- #define NO_ERROR 0
- #define IO_ERROR -1
- #define NULL2 '\0'
-
-
- get_integer(n)
- int *n;
- {
- char string[80];
-
- read_string(string);
- int_convert(string, n);
- }
-
-
-
-
-
- int_convert (ascii_val, result)
- char *ascii_val;
- int *result;
- {
- int sign = 1; /* -1 if negative */
-
- *result = 0; /* value returned to the calling routine */
-
- /* read passed blanks */
-
- while (is_blank(*ascii_val))
- ascii_val++; /* get next letter */
-
- /* check for sign */
-
- if (*ascii_val == '-' || *ascii_val == '+')
- sign = (*ascii_val++ == '-') ? -1 : 1; /* find sign */
-
- /*
- * convert the ASCII representation to the actual
- * decimal value by subtracting '0' from each character.
- *
- * for example, the ASCII '9' is equivalent to 57 in decimal.
- * by subtracting '0' (or 48 in decimal) we get the desired
- * value.
- *
- * if we have already converted '9' to 9 and the next character
- * is '3', we must first multiply 9 by 10 and then convert '3'
- * to decimal and add it to the previous total yielding 93.
- *
- */
-
- while (*ascii_val)
- if (is_digit(*ascii_val))
- *result = *result * 10 + to_decimal(*ascii_val++);
-
- else
- return (IO_ERROR);
-
-
- *result = *result * sign;
-
- return (NO_ERROR);
- }
-
-
-
-
-
- get_short(n)
- short *n;
- {
- char string[80];
-
- read_string(string);
- int_convert(string, n);
- }
-
-
-
-
-
- short_convert (ascii_val, result)
- char *ascii_val;
- short *result;
- {
- int sign = 1; /* -1 if negative */
-
- *result = 0; /* value returned to the calling routine */
-
- /* read passed blanks */
-
- while (is_blank(*ascii_val))
- ascii_val++; /* get next letter */
-
- /* check for sign */
-
- if (*ascii_val == '-' || *ascii_val == '+')
- sign = (*ascii_val++ == '-') ? -1 : 1; /* find sign */
-
- /*
- * convert the ASCII representation to the actual
- * decimal value by subtracting '0' from each character.
- *
- * for example, the ASCII '9' is equivalent to 57 in decimal.
- * by subtracting '0' (or 48 in decimal) we get the desired
- * value.
- *
- * if we have already converted '9' to 9 and the next character
- * is '3', we must first multiply 9 by 10 and then convert '3'
- * to decimal and add it to the previous total yielding 93.
- *
- */
-
- while (*ascii_val){
- if (is_digit(*ascii_val)){
- *result = *result * 10 + to_decimal(*ascii_val++);
- if( (sign == -1) && (*result > 0)) *result = *result * -1;
- }
- else
- return (IO_ERROR);
- } /* ends while ascii_val */
-
- return (NO_ERROR);
- }
-
-
-
-
- get_long(n)
- long *n;
- {
- char string[80];
-
- read_string(string);
- long_convert(string, n);
- }
-
-
-
-
-
- long_convert (ascii_val, result)
- char *ascii_val;
- long *result;
- {
- int sign = 1; /* -1 if negative */
-
- *result = 0; /* value returned to the calling routine */
-
- /* read passed blanks */
-
- while (is_blank(*ascii_val))
- ascii_val++; /* get next letter */
-
- /* check for sign */
-
- if (*ascii_val == '-' || *ascii_val == '+')
- sign = (*ascii_val++ == '-') ? -1 : 1; /* find sign */
-
- /*
- * convert the ASCII representation to the actual
- * decimal value by subtracting '0' from each character.
- *
- * for example, the ASCII '9' is equivalent to 57 in decimal.
- * by subtracting '0' (or 48 in decimal) we get the desired
- * value.
- *
- * if we have already converted '9' to 9 and the next character
- * is '3', we must first multiply 9 by 10 and then convert '3'
- * to decimal and add it to the previous total yielding 93.
- *
- */
-
- while (*ascii_val)
- if (is_digit(*ascii_val))
- *result = *result * 10 + to_decimal(*ascii_val++);
-
- else
- return (IO_ERROR);
-
-
- *result = *result * sign;
-
- return (NO_ERROR);
- }
-
-
-
-
- get_float(f)
- float *f;
- {
- char string[80];
-
- read_string(string);
- float_convert(string, f);
- }
-
-
- float_convert (ascii_val, result)
- char *ascii_val;
- float *result;
- {
- int count; /* # of digits to the right of the
- decimal point. */
- int sign = 1; /* -1 if negative */
-
- double pow10(); /* Turbo C function */
- float power(); /* function returning a value raised
- to the power specified. */
-
- *result = 0.0; /* value desired by the calling routine */
-
- /* read passed blanks */
-
- while (is_blank(*ascii_val))
- ascii_val++; /* get the next letter */
-
- /* check for a sign */
-
- if (*ascii_val == '-' || *ascii_val == '+')
- sign = (*ascii_val++ == '-') ? -1 : 1; /* find sign */
-
-
- /*
- * first convert the numbers on the left of the decimal point.
- *
- * if the number is 33.141592 this loop will convert 33
- *
- * convert ASCII representation to the actual decimal
- * value by subtracting '0' from each character.
- *
- * for example, the ASCII '9' is equivalent to 57 in decimal.
- * by subtracting '0' (or 48 in decimal) we get the desired
- * value.
- *
- * if we have already converted '9' to 9 and the next character
- * is '3', we must first multiply 9 by 10 and then convert '3'
- * to decimal and add it to the previous total yielding 93.
- *
- */
-
- while (*ascii_val)
- if (is_digit(*ascii_val))
- *result = *result * 10 + to_decimal(*ascii_val++);
-
- else if (*ascii_val == '.') /* start the fractional part */
- break;
-
- else
- return (IO_ERROR);
-
-
- /*
- * find number to the right of the decimal point.
- *
- * if the number is 33.141592 this portion will return 141592.
- *
- * by converting a character and then dividing it by 10
- * raised to the number of digits to the right of the
- * decimal place the digits are placed in the correct locations.
- *
- * 4 / power (10, 2) ==> 0.04
- *
- */
-
- if (*ascii_val != NULL2)
- {
- ascii_val++; /* past decimal point */
-
- for (count = 1; *ascii_val != NULL2; count++, ascii_val++)
-
- /*********************************************
- *
- * The following change was made 16 June 1987.
- * For some reason the power function below
- * was not working. Borland's Turbo C pow10
- * was substituted.
- *
- ***********************************************/
- if (is_digit(*ascii_val)){
- *result = *result + to_decimal(*ascii_val)/power(10.0,count);
- /***********
- *result = *result + to_decimal(*ascii_val)/((float)(pow10(count)));
- ************/
- }
-
- else
- return (IO_ERROR);
- }
-
- *result = *result * sign; /* positive or negative value */
-
- return (NO_ERROR);
- }
-
-
- float power(value, n)
- float value;
- int n;
- {
- int count;
- float result;
-
- if(n < 0)
- return(-1.0);
-
- result = 1;
- for(count=1; count<=n; count++){
- result = result * value;
- }
-
- return(result);
- }
-